package indi.h.mlx.sql;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.google.common.base.Optional;

import indi.h.mlx.sql.inter.SQLJoinException;
import indi.h.mlx.sql.inter.SQLRule;
import indi.h.mlx.sql.inter.SQLStructure.SqlType;
import indi.h.mlx.sql.items.SQL;
import indi.h.mlx.sql.items.SQLData;

public class CRUDSqlRule implements SQLRule<List<SQLData>, SQL> {
	// 语句类型
	private SqlType sqlType;

	// 表名
	private Map<String, SQLJoinImp> table;

	// 查询语句查询的值
	private String selectData;

	public static String SELECT_DATA = "select_data";
	public static String TABLE = "table";
	public static String SQL_TYPE = "sql_type";

	@Override
	public SQL rule(List<SQLData> s) {
		// TODO Auto-generated method stub
		if (this.sqlType == SqlType.DELETE_BY_ALL||this.sqlType == SqlType.DELETE_BY_ID)
			return deleteSql();
		if (this.sqlType == SqlType.UPDATE_BY_ID || this.sqlType == SqlType.UPDATE_BY_NONE)
			return updateSql(s);
		if (this.sqlType == SqlType.INSERT)
			return insertSql(s);
		if (this.sqlType == SqlType.SELECT)
			return selectSql();
		return null;
	}

	/**
	 * 创建插入sql语句头
	 * 
	 * @param s
	 * @return
	 */
	private SQL insertSql(List<SQLData> s) {
		StringBuilder prefix = new StringBuilder("insert into " + table.get("").getTableName() + "(");
		StringBuilder data = new StringBuilder(") values(");
		Object[] objects = new Object[s.size()];
		int i = 0;
		for (SQLData sd : s) {
			prefix.append("`" + sd.getName() + "`,");
			data.append("?,");
			objects[i] = sd.getValue();
			i++;
		}
		data.deleteCharAt(data.length() - 1).append(")");
		prefix.deleteCharAt(prefix.length() - 1).append(data);
		return new SQL(prefix.toString(), objects);
	}

	/**
	 * 创建更新sql语句头
	 * 
	 * @param s
	 * @return
	 */
	private SQL updateSql(List<SQLData> s) {
		StringBuilder prefix = new StringBuilder("update " + table.get("").getTableName() + " set ");
		Object[] object = new Object[s.size()];
		int i = 0;
		for (SQLData k : s) {
			prefix.append("`" + k.getName() + "`=?,");
			object[i] = k.getValue();
			i++;
		}
		prefix.deleteCharAt(prefix.length() - 1);
		return new SQL(prefix.toString(), object);
	}

	/**
	 * 创建删除sql语句头
	 * 
	 * @return
	 */
	private SQL deleteSql() {
		StringBuilder prefix = new StringBuilder("delete from " + table.get("").getTableName());
		return new SQL(prefix.toString(), new Object[0]);
	}

	/**
	 * 创建查询sql语句头
	 * 
	 * @return
	 */
	private SQL selectSql() {
		StringBuilder sb = new StringBuilder();
		for (Entry<String, SQLJoinImp> e : this.table.entrySet()) {
			try {
				sb.append(e.getValue().getJoin());
				sb.append(",");
			} catch (SQLJoinException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		sb.deleteCharAt(sb.length() - 1);
		StringBuilder prefix = new StringBuilder("select " + selectData + " from " + sb.toString());
		return new SQL(prefix.toString(), new Object[0]);
	}

	@Override
	public void init(Map<String, Object> m) {
		// TODO Auto-generated method stub
		this.table = (Map<String, SQLJoinImp>) Optional.of(m.get(TABLE)).get();
		this.sqlType = (SqlType) Optional.of(m.get(SQL_TYPE)).get();
		String[] or = (String[]) Optional.fromNullable(m.get(SELECT_DATA)).or(new String[] { "*" });
		StringBuilder sb = new StringBuilder();
		for (String o : or) {
			sb.append(o);
			sb.append(",");
		}
		this.selectData = sb.deleteCharAt(sb.length() - 1).toString();
	}

}
